<template>
  <div class="table-page">
    <GiTable
      row-key="userWechatGroupId"
      v-model:columns="columns"
      :loading="loading"
      :data="tableData"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      @refresh="getTableData"
      :simple="true"
    >
      <template #custom-extra>
        <a-form
          label-align="right"
          layout="inline"
          auto-label-width
          :model="listParams"
          size="small"
          class="form my-10"
        >
          <a-form-item field="logType" label="日志类型">
            <a-radio-group v-model="listParams.logType" @change="changeCurrent(1)" placeholder="请选择" allow-clear>
              <a-radio :value="1">登录</a-radio>
              <a-radio :value="2">充值</a-radio>
              <a-radio :value="3">智选跟群</a-radio>
              <a-radio :value="4">跟圈</a-radio>
              <a-radio :value="5">一键发群</a-radio>
              <a-radio :value="6">一键发圈</a-radio>
              <a-radio :value="7">群管理</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item hide-label>
            <ms-date
              v-model:start="listParams.logAtBegin"
              v-model:end="listParams.logAtEnd"
              :show-quick="true"
              :disabledDate="(current) => dayjs(current).isAfter(dayjs())"
            />
          </a-form-item>
          <a-form-item>
            <a-space>
              <a-button type="primary" size="small" @click="changeCurrent(1)">
                <template #icon>
                  <icon-search />
                </template>
                <template #default>查询</template>
              </a-button>
              <a-button size="small" @click="onResetSearch">
                <template #icon>
                  <icon-sync />
                </template>
                <template #default>重置</template>
              </a-button>
            </a-space>
          </a-form-item>
        </a-form>
      </template>
      <template #followSendFailNum="{ record }">
        <span class="textD43036 font-bold text-underline cursor-pointer">{{ record.followSendFailNum || '0' }}</span>
      </template>
    </GiTable>
  </div>
</template>

<script setup>
import { usePagination } from '@/hooks'
import { detailLogList } from '@/apis'
import dayjs from 'dayjs'

const props = defineProps(['userWechatId'])

const columns = ref([
  {
    title: '日志类型',
    tooltip: true,
    ellipsis: true,
    width: 200,
    render: (data) => {
      const text = data.record.logType
      return text == 1
        ? '登录'
        : text == 2
        ? '充值'
        : text == 3
        ? '智选跟群'
        : text == 4
        ? '跟圈'
        : text == 5
        ? '一键发群'
        : text == 6
        ? '一键发圈'
        : text == 7
        ? '群管理'
        : '其他'
    }
  },
  {
    title: '操作行为',
    tooltip: true,
    ellipsis: true,
    width: 250,
    dataIndex: 'behavior'
  },
  {
    title: '操作详情',
    width: 300,
    tooltip: true,
    ellipsis: true,
    dataIndex: 'logDetail'
  },
  {
    title: '时间',
    width: 180,
    dataIndex: 'logAt',
    align: 'center'
  }
])

const loading = ref(false)
const tableData = ref([])

const { pagination, setTotal } = usePagination(() => getTableData(), { defaultPageSize: 20 })

const listParams = reactive({
  page: 1,
  userWechatId: undefined,
  logType: 1,
  logAtBegin: undefined,
  logAtEnd: undefined
})

// 获取列表数据
const getTableData = async () => {
  try {
    loading.value = true
    listParams.page = pagination.current
    listParams.userWechatId = props.userWechatId
    const res = await detailLogList(listParams)
    tableData.value = res.data.list
    setTotal(res?.data?.meta?.pagination?.total || 0)
  } finally {
    loading.value = false
  }
}

// 表格页码改变
const changeCurrent = (e) => {
  pagination.current = listParams.page = e
  getTableData()
}

watch(
  () => props.userWechatId,
  () => {
    changeCurrent(1)
  },
  { deep: true, immediate: true }
)

// 列表重置搜索
const onResetSearch = () => {
  listParams.logType = listParams.logAtBegin = listParams.logAtEnd = undefined
  changeCurrent(1)
}
</script>

<style lang="scss" scoped>
.table-page {
  height: 100%;
  overflow: hidden;
  background: var(--color-bg-1);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .form {
    :deep(.arco-form-item) {
      margin-bottom: 10px;
    }
  }
}
</style>
